package com.gewu.advertisement

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import com.gewu.advertisement.core.AdvertisementView
import com.gewu.advertisement.core.model.PlayControl
import com.gewu.advertisement.core.model.PlayControlType
import com.gewu.advertisement.core.service.AdvertisementService
import com.gewu.advertisement.core.service.AuthService

class MainActivity : Activity() {
    private val tag = "MainActivity"
    private val advertisementService = AdvertisementService()
    private lateinit var advertisementView: AdvertisementView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
        window.decorView.systemUiVisibility = uiOptions
        advertisementView = findViewById(R.id.advertisementView)

        val authService = AuthService()
        authService.auth(
            "3042101220001",
            "7z71ki8q3pxrs449",
            onSuccess = {
                Log.i(tag, "认证成功:$it")
                advertisementService.addObserver(advertisementView)
                advertisementService.start(it.connectWsUrl)
            },
            onFail = {
                Log.i(tag, "认证失败:${it.message}")
            }
        )
        findViewById<Button>(R.id.pause).setOnClickListener {
            advertisementView.playControl(PlayControl(PlayControlType.PAUSE))
        }
        findViewById<Button>(R.id.start).setOnClickListener {
            advertisementView.playControl(PlayControl(PlayControlType.START))
        }
        findViewById<Button>(R.id.stop).setOnClickListener {
            advertisementView.playControl(PlayControl(PlayControlType.STOP))
        }
        findViewById<Button>(R.id.restart).setOnClickListener {
            advertisementView.playControl(PlayControl(PlayControlType.RESTART))
        }
        findViewById<Button>(R.id.setting).setOnClickListener {
            startActivity(Intent(this, SettingActivity::class.java))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        advertisementService.stop()
    }

    override fun onPause() {
        super.onPause()
        advertisementView.playControl(PlayControl(PlayControlType.PAUSE))
    }

    override fun onResume() {
        super.onResume()
        advertisementView.playControl(PlayControl(PlayControlType.START))
    }
}